package com.data.modules.smzh.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.constant.MsgConstants;
import com.data.common.constant.WebsocketConst;
import com.data.common.constant.enums.DySmsEnum;
import com.data.common.system.query.QueryGenerator;
import com.data.common.system.vo.LoginUser;
import com.data.common.util.*;
import com.data.modules.base.service.BaseCommonService;
import com.data.modules.message.websocket.WebSocket;
import com.data.modules.sm.entity.SmRepairStats;
import com.data.modules.sm.entity.SysConfig;
import com.data.modules.sm.service.ISmRepairStatsService;
import com.data.modules.sm.service.ISysConfigService;
import com.data.modules.smzh.entity.*;
import com.data.modules.smzh.service.*;
import com.data.modules.smzh.vo.BuildReqVo;
import com.data.modules.smzh.vo.SmContactVo;
import com.data.modules.smzh.vo.SmDeviceVo;
import com.data.modules.smzh.vo.SmUserReqVo;
import com.data.modules.system.entity.User;
import com.data.modules.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @Author boot
 */

@RestController
@RequestMapping("/smface-manage/api")
@Api(tags="smface")
@Slf4j
@SuppressWarnings("ALL")
public class RegisterManageController<stringWarnType> {



	@Autowired
	private IFacedeviceService deviceService;

	@Autowired
	private IOftenContactService oftenContactService;

	@Autowired
	private ISmComAnnouncementService smComAnnouncementService;

	@Autowired
	private ISmzhScreenMsgService smzhScreenMsgService;

	@Autowired
	private IBuildingService buildingService;

	@Autowired
	private ISmDeviceLogsService smDeviceLogsService;

	@Autowired
	private ISysUserService sysUserService;

	@Autowired
	private ISmAccountInfoService smAccountInfoService;

	@ApiOperation(value="绑定设备", notes="绑定设备")
	@PostMapping(value = "/v1.0/bindDevice")
	public Result<?> bindDevice(@RequestBody SmDeviceVo smDeviceVo,HttpServletRequest req) {

		return deviceService.doBindDevice(smDeviceVo,req);
	}


	@ApiOperation(value="分页查询设备信息", notes="分页查询设备信息")
	@GetMapping(value = "/v1.0/findAllDevice")
	public Result<?> findAllDevice(SmDeviceVo smDeviceVo,
								   HttpServletRequest req) {

		return deviceService.searchAllDevice(smDeviceVo,req);
	}

	@ApiOperation(value="新增常用电话", notes="新增常用电话")
	@PostMapping(value = "/v1/addContact")
	public Result<?> addContact(@RequestBody SmContactVo smContactVo, HttpServletRequest req) {

		return oftenContactService.addContact(smContactVo,req);
	}

	@AutoLog(value = "小区公告信息-添加")
	@ApiOperation(value="小区公告信息-添加", notes="小区公告信息-添加")
	@PostMapping(value = "/v1/add")
	public Result<?> add(@RequestBody SmComAnnouncement smComAnnouncement , HttpServletRequest req) {

		return smComAnnouncementService.smAddInfo(smComAnnouncement,req);
	}

	@AutoLog(value = "小区公告信息-删除")
	@ApiOperation(value="小区公告信息-删除", notes="小区公告信息-删除")
	@PostMapping(value = "/v1/deleteAdd")
	public Result<?> deleteAdd(@RequestBody SmComAnnouncement smComAnnouncement , HttpServletRequest req) {

		return smComAnnouncementService.smDeleteAdd(smComAnnouncement,req);
	}

	@AutoLog(value = "小区公告信息-查看")
	@ApiOperation(value="小区公告信息-查看", notes="小区公告信息-查看")
	@PostMapping(value = "/v1/queryAddInfo")
	public Result<?> queryAddInfo(@RequestBody SmComAnnouncement smComAnnouncement , HttpServletRequest req) {

		return smComAnnouncementService.smQueryAddInfo(smComAnnouncement,req);
	}

	@ApiOperation(value="查询门口机广告信息", notes="查询门口机广告信息")
	@GetMapping(value = "/v1/queryAdd")
	public Result<?> queryAdd(SmComAnnouncement smComAnnouncement,
								   HttpServletRequest req) {

		return smComAnnouncementService.searchAddInfo(smComAnnouncement,req);
	}

	@AutoLog(value = "推送门口机广告")
	@ApiOperation(value="推送门口机广告", notes="推送门口机广告")
	@PostMapping(value = "/v1/addPush")
	public Result<?> addPush(@RequestBody SmComAnnouncement smComAnnouncement) {

		String id =  smComAnnouncement.getId();
		SmComAnnouncement announcement = smComAnnouncementService.getById(id);
		if(ObjectUtil.isNotEmpty(announcement)){

			//通过项目主键查询出 绑定的 设备序列号推送消息
			List<String> serialNoList =  buildingService.queryDeviceInfoByProjectId(announcement.getComNo());

			serialNoList.parallelStream().forEach(x->{
				List list = Arrays.asList(announcement.getImage().split(","));
				Boolean flag=true;

				flag = KamliClientUtil.doPostpushMsgUr(x,MsgConstants.DOOR_MSG,list);
				log.info("门口机广告推送消息序列号{},结果：{}",x,flag);
			});

		}

		return Result.ok(0,"操作成功");

	}

	@AutoLog(value = "物业公告-删除")
	@ApiOperation(value="物业公告-删除", notes="物业公告-删除")
	@PostMapping(value = "/v1/deleteNotic")
	public Result<?> deleteNotic(@RequestBody SmzhScreenMsg smComAnnouncement , HttpServletRequest req) {

		return smzhScreenMsgService.smDeleteNoticInfo(smComAnnouncement,req);
	}


	@AutoLog(value = "物业公告-查看")
	@ApiOperation(value="物业公告-查看", notes="物业公告-查看")
	@PostMapping(value = "/v1/queryNoticInfo")
	public Result<?> queryNoticInfo(@RequestBody SmzhScreenMsg smComAnnouncement , HttpServletRequest req) {

		return smzhScreenMsgService.smQueryNoticeInfo(smComAnnouncement,req);
	}

	@ApiOperation(value="查询物业公告信息", notes="查询物业公告信息")
	@GetMapping(value = "/v1/queryNoticeList")
	public Result<?> queryNoticeList(SmzhScreenMsg smComAnnouncement,
									 HttpServletRequest req) {

		return smzhScreenMsgService.searchNoticeListInfo(smComAnnouncement,req);
	}

	@AutoLog(value = "物业公告-添加")
	@ApiOperation(value="物业公告-添加", notes="物业公告-添加")
	@PostMapping(value = "/v1/noticAdd")
	public Result<?> noticAdd(@RequestBody SmzhScreenMsg smComAnnouncement , HttpServletRequest req) {

		return smzhScreenMsgService.smAddNoticInfo(smComAnnouncement,req);
	}

	@AutoLog(value = "物业公告-修改")
	@ApiOperation(value="物业公告-修改", notes="物业公告-修改")
	@PostMapping(value = "/v1/noticModity")
	public Result<?> noticModity(@RequestBody SmzhScreenMsg smComAnnouncement , HttpServletRequest req) {

		return smzhScreenMsgService.smModityNoticInfo(smComAnnouncement,req);
	}

	@AutoLog(value = "推送物业公告信息")
	@ApiOperation(value="推送物业公告信息", notes="推送物业公告信息")
	@PostMapping(value = "/v1/noticPush")
	public Result<?> noticPush(@RequestBody SmzhScreenMsg jsonObject) {

		String msgType = MsgConstants.SCREEN_MSG;
		String id =  jsonObject.getId();
		List list = new ArrayList();
		SmzhScreenMsg smzhScreenMsg = smzhScreenMsgService.getById(id);

		Map<String,Object> map = new HashMap<>();
		map.put("title",smzhScreenMsg.getTitle());
		map.put("type",smzhScreenMsg.getType());
		map.put("content",smzhScreenMsg.getRemakr());
		list.add(map);

		if(ObjectUtil.equals(2,smzhScreenMsg.getMsgClass())){//智慧屏公告
			msgType = MsgConstants.NOTICE_MSG;
		}else if(ObjectUtil.equals(3,smzhScreenMsg.getMsgClass())){//文化专栏
			msgType = MsgConstants.CULTURE_MSG;
		}

		List<String> projectlist = Arrays.asList(smzhScreenMsg.getProjectNo().split(","));

		List<Building> buildList= buildingService.queryBuildByProject(projectlist,smzhScreenMsg.getType());

		String finalMsgTyp = msgType;
		/*buildList.stream().forEach(x->{
			//根据操作appId 和项目编号，查询建筑编号，循环推送消息
			KamliClientUtil.doPostpushMsgUr(x.getUniqueNo(), finalMsgTyp,list);
		});*/

		List<Building> buildFifterList = buildList;
		if(ObjectUtil.isNotNull(smzhScreenMsg.getBuildNo())){
			buildFifterList= buildList.stream()
					.filter(x-> x.getBuildNo().equals(smzhScreenMsg.getBuildNo()))
					.collect(Collectors.toList());
		}

		if(ObjectUtil.isNotEmpty(smzhScreenMsg.getUnitNo())){
			buildFifterList= buildFifterList.stream()
					.filter(x->  x.getUnitNo().equals(smzhScreenMsg.getUnitNo()))
					.collect(Collectors.toList());
		}

		if(ObjectUtil.isNotEmpty(smzhScreenMsg.getRoomNo())){
			buildFifterList= buildFifterList.stream()
					.filter(x->  x.getRoomNo().equals(smzhScreenMsg.getRoomNo()))
					.collect(Collectors.toList());
		}

		buildFifterList.stream().forEach(x->{
			//根据操作appId 和项目编号，查询建筑编号，循环推送消息
			KamliClientUtil.doPostpushMsgUr(x.getUniqueNo(), finalMsgTyp,list);
		});

		return Result.ok(0,"操作成功");
	}

	@ApiOperation(value="家庭系统修改密码", notes="家庭系统修改密码")
	@PostMapping(value = "/v1/userUpdatePassword")
	public Result<?> userUpdatePassword(@RequestBody SmUserReqVo userReqVo, HttpServletRequest req) {

		return sysUserService.updatePasswordInfo(userReqVo,req);
	}

	@ApiOperation(value="查询建筑基本信息", notes="查询建筑基本信息")
	@GetMapping(value = "/v1/queryBuildIngInfo")
	public Result<?> queryBuildIngInfo(BuildReqVo buildReqVo,
									   HttpServletRequest req) {

		return buildingService.queryBuildInfo(buildReqVo,req);
	}


	@ApiOperation(value="分页查询报警日志", notes="分页查询报警日志")
	@GetMapping(value = "/v1/queryAlarmInfo")
	public Result<?> queryAlarmInfo(SmDeviceLogs smDeviceLogs, HttpServletRequest req) {

		//smDeviceLogs.setCreateBy("smVip020");

		return smDeviceLogsService.searchAlarmInfo(smDeviceLogs,req);
	}

	@ApiOperation(value="分页查询报修日志", notes="分页查询报修日志")
	@GetMapping(value = "/v1/queryRepairInfo")
	public Result<?> queryRepairInfo(SmDeviceLogs smDeviceLogs, HttpServletRequest req) {

		//smDeviceLogs.setCreateBy("smVip020");

		return smDeviceLogsService.searchRepairInfo(smDeviceLogs,req);
	}


	@ApiOperation(value="查询访客预约", notes="查询访客预约")
	@GetMapping(value = "/v1/queryVisitorInfo")
	public Result<?> queryVisitorInfo(SmDeviceLogs smDeviceLogs, HttpServletRequest req) {

		//smDeviceLogs.setCreateBy("smVip020");

		return buildingService.searchVisitorInfo(smDeviceLogs,req);
	}

	@ApiOperation(value="查询授权信息", notes="查询授权信息")
	@GetMapping(value = "/v1/queryAuthInfo")
	public Result<?> queryAuthInfo(SmAccountInfo smDeviceLogs, HttpServletRequest req) {

		smAccountInfoService.lambdaQuery().eq(ObjectUtil.isNotEmpty(smDeviceLogs.getAccountInfo()),SmAccountInfo::getAccountInfo,smDeviceLogs.getAccountInfo()).list();
		return Result.ok(smAccountInfoService.list());
	}

	@ApiOperation(value="新增授权信息", notes="新增授权信息")
	@PostMapping(value = "/v1/addAuth")
	public Result<?> addAuthInfo(@RequestBody SmAccountInfo smAccountInfo, HttpServletRequest req) {

		smAccountInfoService.save(smAccountInfo);

		return Result.ok(0);
	}

	@ApiOperation(value="删除授权信息", notes="删除授权信息")
	@PostMapping(value = "/v1/deleteAuthById")
	public Result<?> deleteAuthById(@RequestBody SmAccountInfo smAccountInfo, HttpServletRequest req) {

		smAccountInfoService.removeById(smAccountInfo.getId());

		return Result.ok(0);
	}

	@ApiOperation(value="门口机远程重启", notes="门口机远程重启")
	@PostMapping(value = "/v1/sendRebootMsg")
	public Result<?> sendRebootMsg(@RequestBody SmDeviceVersion deviceVersion) {
		String sn = deviceVersion.getSerialNo();
		List<Map> list = new ArrayList<>();
		Map<String,Object> map = new HashMap<>();
		map.put("content","type");
		list.add(map);
		KamliClientUtil.doPostpushMsgUr(sn, MsgConstants.REBOOT_MSG,list);

		return Result.ok(0);
	}

}
